package com.zmj.sy.mom.srv.aps.utils;

import cn.hutool.core.io.FileUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zmj.sy.mom.srv.aps.bean.bo.Agency;
import com.zmj.sy.mom.srv.aps.bean.entity.resource.LogApi;
import com.zmj.sy.mom.srv.aps.bean.entity.resource.LogContent;
import com.zmj.sy.mom.srv.aps.bean.entity.resource.LogSystem;
import com.zmj.sy.mom.srv.aps.mapper.LogApiMapper;
import com.zmj.sy.mom.srv.aps.mapper.LogContentMapper;
import com.zmj.sy.mom.srv.aps.mapper.LogSystemMapper;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.io.File;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@RequiredArgsConstructor
@Component
public class LogApiUtils {

    private final LogApiMapper logApiMapper;
    private final LogSystemMapper logSystemMapper;
    private final LogContentMapper logContentMapper;

    public static final String SERVER_NAME = "mom-srv-aps";

    private static final Map<String, Agency> agencyMap = new HashMap<>();

    static {
        agencyMap.put("sinocam", new Agency("sinocam", "卓畅套料"));
    }

    private final ObjectMapper objectMapper;

    public LogApi getSendLog(String receiver, String apiName, String url) {
        LogApi logApi = new LogApi();
        logApi.setAppId(SERVER_NAME);
        logApi.setDir(1);
        logApi.setSender(SERVER_NAME);
        logApi.setReceiver(receiver);
        logApi.setApiName(apiName);
        logApi.setSuccess(false);
        logApi.setCallDateTime(LocalDateTime.now());
        logApi.setUrl(url);
        logApi.setWorkTime(System.currentTimeMillis());
        return logApi;
    }

    public LogApi getReceiveLog(String sender, String apiName, String url) {
        LogApi logApi = new LogApi();
        logApi.setAppId(SERVER_NAME);
        logApi.setDir(2);
        logApi.setSender(sender);
        logApi.setReceiver(SERVER_NAME);
        logApi.setApiName(apiName);
        logApi.setSuccess(false);
        logApi.setCallDateTime(LocalDateTime.now());
        logApi.setUrl(url);
        logApi.setWorkTime(System.currentTimeMillis());
        return logApi;
    }


    public LogApi setReq(LogApi logApi, Object object) {
        logApi.setRequestPath(JsonUtils.toJsonString(object));
        return logApi;
    }

    public LogApi setReqStr(LogApi logApi, String object) {
        logApi.setRequestPath(object);
        return logApi;
    }

    public LogApi successAndSave(LogApi logApi, Object object) {
        return save(true, logApi, object, null);
    }

    public LogApi successAndSave(LogApi logApi, Object object, String remark) {
        return save(true, logApi, object, remark);
    }


    public LogApi errorAndSave(LogApi logApi, Object object, String e) {
        return save(false, logApi, object, e);
    }

    @SneakyThrows
    public LogApi save(Boolean success, LogApi logApi, Object object, String e) {
        logApi.setSuccess(success);
        if (object != null) {
            logApi.setResponsePath(JsonUtils.toJsonString(object));
        }
        logApi.setWorkTime(System.currentTimeMillis() - logApi.getWorkTime());
        logApi.setRemark(e);
        if(StringUtils.hasText(logApi.getRemark()) && logApi.getRemark().length() > 100){
            logApi.setRemark(logApi.getRemark().substring(0, 100));
        }

        String reqStr = logApi.getRequestPath();
        String resStr = logApi.getResponsePath();
        if(resStr == null){
            resStr = "";
        }

        reqStr = StringEscapeUtils.unescapeJava(reqStr);
        resStr = StringEscapeUtils.unescapeJava(resStr);

        String reqPath = FileStorageUtils.getFilePath(FileStorageUtils.LOG_API_TYPE);
        String resPath = FileStorageUtils.getFilePath(FileStorageUtils.LOG_API_TYPE);

        FileUtil.writeString(reqStr, new File(reqPath), "UTF-8");
        FileUtil.writeString(resStr, new File(resPath), "UTF-8");

        logApi.setRequestPath(reqPath);
        logApi.setResponsePath(resPath);
        logApi.setSuccess(success);

        logApiMapper.insert(logApi);
        return logApi;
    }

    public LogApi saveDB(Boolean success, LogApi logApi, String result, Integer logType) {
        String req = logApi.getRequestPath() != null ? logApi.getRequestPath() : "";
        result = result != null ? result : "";
        logApi.setSuccess(success);
        logApi.setWorkTime(System.currentTimeMillis() - logApi.getWorkTime());
        logApi.setRequestPath(null);
        LogSystem logsystem = new LogSystem();
        BeanUtils.copyProperties(logApi, logsystem);
        logsystem.setQueryParam(req.length() > WorkshopProp.LOG_SIZE ? req.substring(0, WorkshopProp.LOG_SIZE ) : req);
        logsystem.setResponsePath(result.length() > WorkshopProp.LOG_SIZE  ? result.substring(0, WorkshopProp.LOG_SIZE ) : result);
        logSystemMapper.insert(logsystem);
        if (req.length() > WorkshopProp.LOG_SIZE  || result.length() > WorkshopProp.LOG_SIZE ) {
            LogContent con = new LogContent();
            con.setReqContent(req);
            con.setResContent(result);
            con.setLogType(logType);
            con.setLogId(logApi.getId());
            logContentMapper.insert(con);
        }
        return logApi;
    }

}
